<template>
  <div>
    <el-row :gutter="24">
      <el-col :span="4">
        <!-- <div style="padding:20px"> -->

        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>组织架构</span>
          </div>
          <el-tree
            ref="treeBox"
            :data="deptTree"
            :props="defaultProps"
            :expand-on-click-node="false"
            default-expand-all
            :highlight-current="true"
            node-key="departmentId"
            @node-click="handleNodeClick"
          />
        </el-card>
      <!-- </div> -->
      </el-col>

      <!--卡片-->
      <el-col :span="20">
        <el-card class="box-card">
          <!-- 搜索与添加区域 -->
          <el-button v-hasPermi="['admin','system:user:add']" type="primary" small icon="el-icon-plus" class="addBtn" style="margin:10px" size="mini" @click="addDialogVisible = true">新增
          </el-button>

          <el-button v-hasPermi="['admin','system:user:import']" type="info" small icon="el-icon-upload2" class="importBtn" style="margin:10px" size="mini" @click="importDialogVisible = true">导入
          </el-button>

          <el-button v-hasPermi="['admin','system:user:export']" type="warning" small icon="el-icon-download" class="exportBtn" style="margin:10px" size="mini" @click="exportClick">导出
          </el-button>

          <!--用户表格-->
          <el-table :data="listForm" stripe border style="width: 100%">
            <el-table-column prop="userId" label="ID" width="80" />
            <el-table-column prop="userName" label="用户名" width="180" />
            <el-table-column prop="nickName" label="昵称" />
            <el-table-column prop="dept.deptName" label="部门" />
            <el-table-column prop="sex" label="性别">
              <template slot-scope="scope">
                {{ scope.row.sex | sexFormat }}
              </template>
            </el-table-column>

            <el-table-column label="状态">
              <template slot-scope="scope">
                <el-switch
                  v-model="scope.row.status"
                  active-color="#13ce66"
                  inactive-color="#ff4949"
                  :active-value="1"
                  :inactive-value="0"
                  @change="userStateChanged(scope.row)"
                /></template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" />
            <el-table-column prop="createTime" label="日期" width="180" />
            <!--操作-->
            <el-table-column label="操作" width="130">
              <template slot-scope="scope">
                <el-button v-hasPermi="['admin','system:user:edit']" type="text" size="mini" @click="editBtn(scope.row.userId)">编辑</el-button>
                <el-button v-hasPermi="['admin','system:user:del']" type="text" size="mini" @click="delBtn(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>

          <!--分页区域-->
          <el-pagination
            background
            :current-page="pageNumber"
            :page-sizes="[1, 2, 5, 10]"
            :page-size="pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />

        </el-card>

        <!--添加用户对话框-->
        <el-dialog title="新增用户" :close-on-click-modal="false" :visible.sync="addDialogVisible" width="580px" @close="addHandleClose">
          <span>
            <el-form ref="addFormRef" :inline="true" :model="addForm" :rules="addFormRules" label-width="70px">
              <el-form-item label="账号" prop="userName">
                <el-input v-model="addForm.userName" />
              </el-form-item>
              <el-form-item label="密码" prop="passWord">
                <el-input v-model="addForm.passWord" />
              </el-form-item>

              <el-form-item label="邮箱" prop="email">
                <el-input v-model="addForm.email" />
              </el-form-item>
              <el-form-item label="性别">
                <el-radio-group v-model="addForm.sex">
                  <el-radio :label="1">男</el-radio>
                  <el-radio :label="0">女</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="昵称" prop="nickName">
                <el-input v-model="addForm.nickName" />
              </el-form-item>
              <el-form-item label="备注" prop="remark">
                <el-input v-model="addForm.remark" />
              </el-form-item>
              <el-form-item label="角色">
                <el-select v-model="addForm.roleIdList" multiple placeholder="请选择角色" style="width: 450px">
                  <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
                </el-select>

              </el-form-item>
              <el-form-item label="部门">
                <el-cascader
                  v-model="addForm.deptIdList"
                  :options="deptTree"
                  :props="defaultProps"
                  style="width: 450px"
                />
              </el-form-item>
            </el-form>

            <!--底部区域-->
          </span>
          <span slot="footer" class="dialog-footer">
            <el-button @click="addDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addClick">确 定</el-button>
          </span>
        </el-dialog>

        <!--编辑用户对话框-->
        <el-dialog title="编辑用户" :close-on-click-modal="false" :visible.sync="editDialogVisible" width="580px" @close="editHandleClose">
          <span>
            <el-form ref="editFormRef" :inline="true" :model="editForm" label-width="70px">
              <el-form-item label="账号" prop="userName">
                <el-input v-model="editForm.userName" disabled />
              </el-form-item>
              <el-form-item label="昵称" prop="nickName">
                <el-input v-model="editForm.nickName" />
              </el-form-item>
              <el-form-item label="邮箱" prop="email">
                <el-input v-model="editForm.email" />
              </el-form-item>
              <el-form-item label="备注" prop="remark">
                <el-input v-model="editForm.remark" />
              </el-form-item>
              <el-form-item label="角色">
                <el-select v-model="editForm.roleIdList" multiple placeholder="请选择角色" style="width: 450px">
                  <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId" />
                </el-select>
              </el-form-item>
              <el-form-item label="部门">
                <el-cascader
                  v-model="editForm.deptIdList"
                  :options="deptTree"
                  :props="defaultProps"
                  style="width: 450px"
                />
              </el-form-item>
            </el-form>
            <!--底部区域-->
          </span>
          <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="editClick">确 定</el-button>
          </span>
        </el-dialog>

        <!--导入对话框-->
        <el-dialog title="导入" :close-on-click-modal="false" :visible.sync="importDialogVisible" width="580px">
          <span>
            <el-upload
              class="upload-demo"
              drag
              action="123"
              :http-request="myUpload"
            >
              <i class="el-icon-upload" />
              <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
              <div slot="tip" class="el-upload__tip">只能上传excel文件</div>
            </el-upload>
            <!--底部区域-->
          </span>
          <span slot="footer" class="dialog-footer">
            <el-button @click="importDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="importClick">确 定</el-button>
          </span>
        </el-dialog>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { add, edit, del, listUser, getUser, importUser, exportUser, editStatus } from '@/api/system/user'
import { listRole } from '@/api/system/role'
import { treeDept } from '@/api/system/dept'
import { downloadFile } from '@/utils/index'
export default {
  name: 'User',
  data() {
    return {
      addDialogVisible: false,
      editDialogVisible: false,
      allotDialogVisible: false,
      importDialogVisible: false,
      pageNumber: 1,
      pageSize: 10,
      total: 0,
      currentDept: 0,
      loading: false,
      deptTree: [],
      defaultProps: {
        checkStrictly: true,
        children: 'children',
        label: 'deptName',
        value: 'deptId'
      },
      listForm: [],
      addForm: {
        userName: '',
        passWord: '',
        deptIdList: [],
        deptId: undefined,
        email: '',
        sex: 1,
        nickName: '',
        remark: '',
        roleIdList: []
      },
      roleList: [],
      editForm: {},
      addFormRules: {
        userName: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            min: 3,
            max: 10,
            message: '用户名的长度在3~10个字符之间',
            trigger: 'blur'
          }
        ],
        passWord: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '用户名的长度在6~15个字符之间',
            trigger: 'blur'
          }
        ],
        email: [
          { required: true, message: '请输入用户邮箱', trigger: 'blur' }
          // { validator: checkEmail, trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    ...mapGetters([
      'user'
    ])
  },
  created() {
    // 初始化用户列表
    this.initListUser(this.user.deptId)
    this.initListRole()
    this.initListDept()
  },

  methods: {

    // 切换部门
    handleNodeClick(data) {
      this.currentDept = data.deptId
      this.$nextTick(() => {
        // treeBox 元素的ref   value 绑定的node-key
        this.$refs.treeBox.setCurrentKey(this.deptTree[0].departmentId)
      })
      this.initListUser(this.currentDept)
    },

    // 由于element 上传组件有跨域问题，使用自定义创建表单发送请求
    myUpload(content) {
      const formData = new FormData()
      formData.append('file', content.file) // 'file[]' 代表数组 其中`file`是可变的
      importUser(formData).then(res => {
        //
        this.$message.success(res.message)
      }).catch(err => {
        // 失败
        console.log(err)
      })
    },

    // 添加按钮
    addBtn() {
      this.addDialogVisible = true
    },

    // 确认添加用户
    addClick() {
      this.addForm.deptId = this.addForm.deptIdList[this.addForm.deptIdList.length - 1]
      this.$refs.addFormRef.validate(valid => {
        if (!valid) return
        add(this.addForm).then(res => {
          this.$message.success(res.message)
          // 刷新数据
          this.initListUser(this.addForm.deptId)

          // 关闭对话框
          this.addDialogVisible = false
        })
      })
    },

    // 编辑按钮
    editBtn(id) {
      this.editDialogVisible = true
      getUser(id).then(res => {
        if (res.code === 20000) {
          this.editForm = res.data
        }
      })
    },

    // 确认编辑用户
    editClick() {
      this.editForm.deptId = this.editForm.deptIdList[this.editForm.deptIdList.length - 1]
      this.$refs.editFormRef.validate(valid => {
        if (!valid) return
        edit(this.editForm).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListUser(this.editForm.deptId)
          }
          // 关闭对话框
          this.editDialogVisible = false
        })
      })
    },

    // 修改状态
    userStateChanged(user) {
      editStatus(user).then(res => {
        if (res.code === 20000) {
          this.$message.success(res.message)
          // 刷新数据
          this.initListUser(this.currentDept)
        }
      })
    },

    // 删除按钮
    delBtn(params) {
      const deptId = params.dept.deptId
      const userId = params.userId
      this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        del(userId, deptId).then(res => {
          if (res.code === 20000) {
            this.$message.success(res.message)
            // 刷新数据
            this.initListUser(deptId)
          }
        })
      })
    },

    // 导入按钮
    importBtn() {
      this.importDialogVisible = true
    },
    // 导入按钮
    importClick() {
      this.importDialogVisible = false
    },

    // 导出
    exportClick() {
      const tableName = 'user'
      this.$confirm('是否确认导出所有用户数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        // 确认操作
        exportUser().then(data => {
          downloadFile(data, tableName, 'xlsx')
        })
      })
    },

    // 监听当前页的事件
    handleCurrentChange(pageIndex) {
      this.pageNumber = pageIndex
      this.initListUser(this.currentDept)
    },

    // 监听当前页的条数事件
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.initListUser(this.currentDept)
    },

    // 监听添加用户对话框的关闭事件，重置表单内容
    addHandleClose() {
      this.$refs.addFormRef.resetFields()
      this.addForm.roleIdList = []
    },

    // 监听编辑用户对话框的关闭事件,重置表单内容
    editHandleClose() {
      this.$refs.editFormRef.resetFields()
      this.addForm.roleIdList = []
    },
    // 初始化用户数据
    initListUser(deptId) {
      // 根据用户的部门id来查用户列表
      const params = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize,
        currentDept: deptId
      }
      listUser(params).then(res => {
        this.listForm = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
        this.currentDept = deptId
      })
    },
    // 初始化角色数据
    initListRole() {
      const params = {
        pageNum: this.pageNumber,
        pageSize: this.pageSize
      }
      listRole(params).then(res => {
        this.roleList = res.data.records
        this.total = res.data.total
        this.pageNum = res.data.current
      })
    },
    // 初始化角色数据
    initListDept() {
      treeDept().then(res => {
        this.deptTree = res.data
      })
    }
  }
}
</script>

<style scoped>

/deep/ .el-tree-node:focus > .el-tree-node__content {
  background-color:rgba(35, 220, 205, 0.78);
    /* background-color:rgba(35, 137, 220, 0.78); */
  }
</style>
